<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="账号来源,字典主键id" prop="dictionaryType">
        <el-select v-model="formData.dictionaryType" placeholder="请选择账号来源,字典主键id">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="原文章id" prop="articleId">
        <el-input v-model="formData.articleId" placeholder="请输入原文章id" />
      </el-form-item>
      <el-form-item label="原文章relate_article_gen_uuid对应的murmurhash值，用来查询多媒体表internet_article_media对应的ID" prop="articleGenUuidHashId">
        <el-input v-model="formData.articleGenUuidHashId" placeholder="请输入原文章relate_article_gen_uuid对应的murmurhash值，用来查询多媒体表internet_article_media对应的ID" />
      </el-form-item>
      <el-form-item label="文章url" prop="url">
        <el-input v-model="formData.url" placeholder="请输入文章url" />
      </el-form-item>
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入文章标题" />
      </el-form-item>
      <el-form-item label="标题翻译" prop="titleCn">
        <el-input v-model="formData.titleCn" placeholder="请输入标题翻译" />
      </el-form-item>
      <el-form-item label="文章类型: 1-原创 2-转发 3-引用 4-评论" prop="articleType">
        <el-select v-model="formData.articleType" placeholder="请选择文章类型: 1-原创 2-转发 3-引用 4-评论">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="内容文本" prop="content">
        <Editor v-model="formData.content" height="150px" />
      </el-form-item>
      <el-form-item label="翻译文本" prop="contentCn">
        <el-input v-model="formData.contentCn" placeholder="请输入翻译文本" />
      </el-form-item>
      <el-form-item label="发文时间戳(秒)" prop="publishedTime">
        <el-date-picker
          v-model="formData.publishedTime"
          type="date"
          value-format="x"
          placeholder="选择发文时间戳(秒)"
        />
      </el-form-item>
      <el-form-item label="发文位置" prop="location">
        <el-input v-model="formData.location" placeholder="请输入发文位置" />
      </el-form-item>
      <el-form-item label="文章作者的唯一标识" prop="userGenUuid">
        <el-input v-model="formData.userGenUuid" placeholder="请输入文章作者的唯一标识" />
      </el-form-item>
      <el-form-item label="文章UUID" prop="articleGenUuid">
        <el-input v-model="formData.articleGenUuid" placeholder="请输入文章UUID" />
      </el-form-item>
      <el-form-item label="多媒体列表" prop="mediaList">
        <el-input v-model="formData.mediaList" placeholder="请输入多媒体列表" />
      </el-form-item>
      <el-form-item label="所属人员ID" prop="personId">
        <el-input v-model="formData.personId" placeholder="请输入所属人员ID" />
      </el-form-item>
      <el-form-item label="标签列表" prop="tags">
        <el-input v-model="formData.tags" placeholder="请输入标签列表" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { InternetArticleApi, InternetArticleVO } from '@/api/member/internetarticle'

/** 互联网文章 表单 */
defineOptions({ name: 'InternetArticleForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  dictionaryType: undefined,
  articleId: undefined,
  articleGenUuidHashId: undefined,
  url: undefined,
  title: undefined,
  titleCn: undefined,
  articleType: undefined,
  content: undefined,
  contentCn: undefined,
  publishedTime: undefined,
  location: undefined,
  userGenUuid: undefined,
  articleGenUuid: undefined,
  mediaList: undefined,
  personId: undefined,
  tags: undefined
})
const formRules = reactive({
  dictionaryType: [{ required: true, message: '账号来源,字典主键id不能为空', trigger: 'change' }],
  articleId: [{ required: true, message: '原文章id不能为空', trigger: 'blur' }],
  articleGenUuidHashId: [{ required: true, message: '原文章relate_article_gen_uuid对应的murmurhash值，用来查询多媒体表internet_article_media对应的ID不能为空', trigger: 'blur' }],
  userGenUuid: [{ required: true, message: '文章作者的唯一标识不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await InternetArticleApi.getInternetArticle(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as InternetArticleVO
    if (formType.value === 'create') {
      await InternetArticleApi.createInternetArticle(data)
      message.success(t('common.createSuccess'))
    } else {
      await InternetArticleApi.updateInternetArticle(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    dictionaryType: undefined,
    articleId: undefined,
    articleGenUuidHashId: undefined,
    url: undefined,
    title: undefined,
    titleCn: undefined,
    articleType: undefined,
    content: undefined,
    contentCn: undefined,
    publishedTime: undefined,
    location: undefined,
    userGenUuid: undefined,
    articleGenUuid: undefined,
    mediaList: undefined,
    personId: undefined,
    tags: undefined
  }
  formRef.value?.resetFields()
}
</script>